package com.lagou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.mapper.ArticleMapper;
import com.lagou.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BlogController {

    @Autowired
    private ArticleMapper articleMapper;

    @RequestMapping("/myblog")
    public String toLoginPage(Model model){

        PageHelper.startPage(1,2);
        List<Article> articles = articleMapper.selectAllArticle();
        model.addAttribute("articles", articles);
        model.addAttribute("pageNum", 1);
        return "client/index";
    }

    @RequestMapping("/next")
    public String toNextPage(Model model, @RequestParam("pageNum") Integer pageNum){

        PageHelper.startPage(pageNum,2);
        List<Article> articles = articleMapper.selectAllArticle();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);

        model.addAttribute("articles", articles);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        return "client/index";
    }

}
